﻿using P2P.Client.Enums;
using P2P.Client.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P2P.Main.Session
{
    public class P2PActiveNodeSessionManager
    {
        private static readonly object _lock = new object();

        private static P2PActiveNodeSessionManager? instance;

        private P2PActiveNodeSessionManager()
        {
            InitTask();
        }

        public static P2PActiveNodeSessionManager Instance
        {
            get 
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        if (instance == null)
                        {
                            instance = new P2PActiveNodeSessionManager();
                        }
                    }
                }
                return instance;
            }
        }

        public ConcurrentDictionary<string, P2PActiveNode> ActiveNodeDict = new ConcurrentDictionary<string, P2PActiveNode>();

        public P2PActiveNode? Find(string name) 
        {
            if (!string.IsNullOrEmpty(name) && ActiveNodeDict.ContainsKey(name))
            {
                return ActiveNodeDict[name];
            }
            else 
            {
                return null;
            }
        }

        public P2PActiveNode? Flush(string name,NatType natType)
        {
            lock (ActiveNodeDict) 
            {
                P2PActiveNode? node = null;

                ActiveNodeDict.TryGetValue(name, out node);

                if (node != null) 
                {
                    node.NatType = natType;
                }
                return node;
            }
        }

        public P2PActiveNode? Flush(string name, string remoteIP,int remotePort)
        {
            lock (ActiveNodeDict)
            {
                P2PActiveNode node = null;

                ActiveNodeDict.TryGetValue(name,out node);

                if (node != null)
                {
                    node.LastActiveTime = DateTime.Now;
                }
                else 
                {
                    node = new P2PActiveNode();
                    node.Name = name;
                    node.IP = remoteIP;
                    node.Port = remotePort;
                    node.LastActiveTime = DateTime.Now;
                    ActiveNodeDict.TryAdd(name, node);
                }
                return node;
            }
        }

        public void InitTask() 
        {
            Task.Run(async () =>
            {
                while (true) 
                {
                    ICollection<P2PActiveNode> nodes = ActiveNodeDict.Values;

                    foreach (P2PActiveNode node in nodes)
                    {
                        DateTime now = DateTime.Now;

                        if (now.Ticks - node.LastActiveTime.Ticks > 30 * 1000)
                        {
                            nodes.Remove(node);
                        }
                    }

                    await Task.Delay(1000);
                }
            });
        } 
    }
}
